package com.weifeng.jdsignin.jd;

import com.weifeng.jdsignin.jd.cookie.JdCookie;
import com.weifeng.jdsignin.jd.head.JdPlusSignInHead;
import com.weifeng.jdsignin.jd.poolcon.PoolCon;
import com.weifeng.jdsignin.log.ConfigLog;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

/**
 * @description：TODO
 * @USER ：weifeng
 * @date ：2022/11/14 下午 03:55
 */
public class JdSignIn  extends ConfigLog {
    private static Logger logger = Logger.getLogger(JdPlusSignin.class);
    private static String url = "https://api.m.jd.com/client.action?functionId=signBeanAct";
    private static String parmeter = "body=%7B%22rnVersion%22%3A%224.7%22%2C%22fp%22%3A%22-1%22%2C%22eid%22%3A%22%22%2C%22shshshfp%22%3A%22-1%22%2C%22userAgent%22%3A%22-1%22%2C%22shshshfpa%22%3A%22-1%22%2C%22referUrl%22%3A%22-1%22%2C%22jda%22%3A%22-1%22%7D&build=168158&client=apple&clientVersion=11.1.2&d_brand=apple&d_model=iPhone13%2C2&ef=1&eid=eidI08f18122b1saFXxwV4hTTVWtq1D8vrv3dBV8cc0BkX3DXoygudf8yzTHpL%2BUjP5n4Wxq6p%2BCNNHnoTYUoC0I5UFrACagPiIxt3R%2BguA6Ewuy72xy&ep=%7B%22ciphertype%22%3A5%2C%22cipher%22%3A%7B%22screen%22%3A%22CJO3CMeyDJCy%22%2C%22wifiBssid%22%3A%22DQC1CWG0CWC2D2C5DJrrEWCzCJC3DtGnEWDwCzSzDQO%3D%22%2C%22osVersion%22%3A%22CJGkCs4n%22%2C%22area%22%3A%22CV8yEJKnXzU1DJU3XzK%3D%22%2C%22openudid%22%3A%22YtHuYwY0YJG0CJKyCwO4CJZvCQCzDzO5DNZtCWPwCwG2ZNYzYJU3DG%3D%3D%22%2C%22uuid%22%3A%22aQf1ZRdxb2r4ovZ1EJZhcxYlVNZSZz09%22%7D%2C%22ts%22%3A1658114261%2C%22hdid%22%3A%22JM9F1ywUPwflvMIpYPok0tt5k9kW4ArJEU3lfLhxBqw%3D%22%2C%22version%22%3A%221.0.3%22%2C%22appname%22%3A%22com.360buy.jdmobile%22%2C%22ridx%22%3A-1%7D&ext=%7B%22prstate%22%3A%220%22%2C%22pvcStu%22%3A%221%22%7D&isBackground=N&joycious=66&lang=zh_CN&networkType=wifi&networklibtype=JDNetworkBaseAF&partner=apple&rfs=0000&scope=01&sign=51d1942ad593e8b31714bddce0c09302&st=1658114320474&sv=100&uemps=0-0&uts=0f31TVRjBSshVsyYjTi8qLyLUZc8ebFgNBA8CJuy7fCqP%2BwqLeB2raCSuMYFHsLI8noR5fLWMWx2505bqoXOdAMgyYaW7Q9k4gAvtY5X0SW4uGAF2ijS6MxiVJEy4pNoIcz4jhTM5ZfWTJtay8SHZQRKOo7j5nEaqXwqWYcM29a2uPf2%2B/zdOEd%2BrzMJhW1F6/3EoJ9JpEpBAnEq8yAiwQ%3D%3D";

    public static void jdsignin(String email, String pt_pin, String pwdt_id, String pt_key) throws IOException {
        CloseableHttpClient httpClient =
                HttpClients.custom().setConnectionManager(PoolCon.poolCon()).build();
        HttpPost httpPost = new HttpPost(url);

        HashMap<String, String> headMap = JdPlusSignInHead.headMap();
        Iterator<String> header = headMap.keySet().iterator();
        while (header.hasNext()) {
            String key = header.next();
            httpPost.setHeader(key, headMap.get(key));
        }

        //从excel中取出参数  添加到请求参数中
        HashMap<String, String> cookieMap = JdCookie.cookieMap();
        logger.info("读取到的 email：" + email);
        cookieMap.put("pt_pin", pt_pin);
        logger.info("读取到的 pt_pin：" + pt_pin);
        cookieMap.put("pwdt_id", pwdt_id);
        logger.info("读取到的 getPwdt_id：" + pwdt_id);
        cookieMap.put("pt_key", pt_key);
        logger.info("读取到的 pt_key：" + pt_key);
        //把 map 转成string，但是 收尾 有 {}，先把 {} 去掉再放到cookie中
        String s = cookieMap.toString().replace(",",";");

        httpPost.setHeader("cookie", s.substring(1, s.length() - 1));

        httpPost.setEntity(new StringEntity(parmeter));

        CloseableHttpResponse execute = httpClient.execute(httpPost);

        System.out.println(EntityUtils.toString(execute.getEntity()));
    }
}
